$(document).ready(function () {
	var registerInfo = {
		username: null,
		phone: null,
		pwd: null
	};
	var checkPass = {
		username: false,
		phone: false,
		pwd: false,
		cfm: false
	}

	//input blur事件
	$('#register_form input').on('blur', function (e) {
		var $target = $(e.currentTarget);
		var val = $target.val();
		var flag = false;
		var txt = '';
		switch ($target.attr('id')) {
			case 'username':
				if (val === '') {
					txt = '请正确填写用户名';
					flag = true;
				} else {
					registerInfo.username = val;
					checkPass.username = true;
				}
				break;
			case 'phone':
				if (val === '' || !(/^(0|86|17951)?(13[0-9]|15[012356789]|18[0-9]|14[57])[0-9]{8}$/.test(val))) {
					txt = '请正确填写手机号';
					flag = true;
				} else {
					registerInfo.phone = val;
					checkPass.phone = true;
				}
				break;
			case 'password':
				if (val === '') {
					txt = '请正确填写密码';
					flag = true;
				} else {
					registerInfo.pwd = val;
					checkPass.pwd = true;
				}
				break;
			case 'confirm_password':
				if (val === '' || val !== $('#password').val()) {
					txt = '确认密码不正确';
					flag = true;
				} else {
					checkPass.cfm = true;
				}
				break;
			default:
				break;
		}
		if (flag) {
			$target.siblings('span').removeClass('correct').addClass('error');
			$('#error_tip').text(txt).css('opacity', 1);
			setTimeout(function () {
				$('#error_tip').css('opacity', 0);
			}, 2000);
		} else {
			$target.siblings('span').removeClass('error').addClass('correct');
		}
	});
	//注册btn click事件
	$('#register_btn').on('click', function (e) {
		var isPass = checkPass.username && checkPass.phone && checkPass.pwd && checkPass.cfm;
		if (isPass) {
			//mock
			var userInfo =JSON.parse(localStorage.getItem('userinfo')) || [];
			userInfo.push(registerInfo);
			localStorage.setItem('userinfo', JSON.stringify(userInfo));
		} else {
			$('#register_failed').modal({});
			return false;
		}
	});
});